package cn.oslanka.mls.sql;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by cnn on 2015/7/1.
 */
public class SaveAndSelect {
    private static SaveAndSelect saveAndSelect;
    public static SaveAndSelect getInstance() {
        if (saveAndSelect==null){
            saveAndSelect = new SaveAndSelect();
        }
        return saveAndSelect;
    }
    public void save(String beanName,int count){
        SQLiteDatabase db =  new MySqlitehelper().getReadableDatabase();
        try {
            StringBuffer sb = new StringBuffer().append("insert into savecount(beanname,count) values (\"").append(beanName).append("\",").append(count).append(")");
            db.execSQL(sb.toString());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            db.close();
        }
    }

    public int select(String beanName){
        SQLiteDatabase db =  new MySqlitehelper().getReadableDatabase();
        int count = 0;
        try {
            Cursor cursor = db.query("savecount", new String[]{"_id", "count"}, "beanname = ?", new String[]{beanName}, null, null, null, null);
            while (cursor.moveToNext()){
                count = cursor.getInt(cursor.getColumnIndex("count"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            db.close();
        }
        return  count;
    }

    public void update(String beanName,int count){
        SQLiteDatabase db =  new MySqlitehelper().getReadableDatabase();
        try {
            ContentValues values = new ContentValues();
            values.put("count",count);
            db.update("savecount", values, "beanname = ?", new String[]{beanName});
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            db.close();
        }
    }

}
